package com.google.code.jmarias.common;

import com.google.code.jmarias.common.Card;
import com.google.code.jmarias.common.Suit;
import com.google.code.jmarias.common.Value;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Trida predstavuje balicek karet. Momentalne to je balicek karet pro marias,
 * ale v budoucnu by mohl byt abstraktni a rozsirovat by jej mohly i jine balicky.
 *
 * Neni thread safe.
 *
 * @author Ondrej Michal
 */
public class CardDeck {

    private List<Card> cards = new ArrayList<Card>();

    /** Vytvori novy balicek zamichanych karet. */
    public CardDeck() {
        for (Suit suit : Suit.values()) {
            for (Value value : Value.values()) {
                cards.add(new Card(suit, value));
            }
        }
        shuffle();
    }

    /** Zamicha karty, ktere jeste v balicku zustaly. */
    public void shuffle() {
        List<Card> result = new ArrayList<Card>();
        Random random = new Random();
        while (! cards.isEmpty()) {
            int randInt = random.nextInt(cards.size());
            result.add(cards.get(randInt));
            cards.remove(randInt);
        }
        cards = result;
    }

    public Card[] getCards(int count) {
        Card[] result = new Card[count];
        for (int i = 0; i < count; i++) {
            result[i] = cards.get(0);
            cards.remove(0);
        }
        return result;
    }
}
